package com.yx.filesystem.monitor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import com.yx.filesystem.client.StorageClientManager;

public class StorageMonitor {

	public StorageMonitor() {
		// TODO 自动生成的构造函数存根
	}

	protected Shell shell;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {			
			StorageMonitor window = new StorageMonitor();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		if(!createContents(display))return;
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected boolean createContents(Display display) {
		shell = new Shell(display, SWT.MAX|SWT.CLOSE | SWT.MIN);
		shell.setText("云想科技StorageMonitor - 用于监控分布式文件系统");
		shell.setLayout(new FillLayout());
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		scrolledComposite.setLayout(new FillLayout());
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		boolean serverManagerStart=false;
		while(!serverManagerStart){
			FileDialog filedlg = new FileDialog(shell, SWT.OPEN);
			filedlg.setText("请选择服务器列表清单文件：");
			filedlg.setFilterPath("./");
			String selected = filedlg.open();
			if(selected==null){
				shell.close();
				return false;
			}
			String[] servers=readConfigFile(selected);
			if(servers.length>0){
				try{
					StorageClientManager.init(servers,32,32);
					serverManagerStart=true;
				}catch(Exception e){
					MessageBox messageBox = new MessageBox(shell, SWT.OK);
					messageBox.setMessage("错误："+e);
					messageBox.open();
				}
			}
		}
		ServerNodeGroup serverNodeGroup=new ServerNodeGroup(scrolledComposite,SWT.NONE);		
		scrolledComposite.setContent(serverNodeGroup);
		//scrolledComposite.setSize(container.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolledComposite.setMinSize(serverNodeGroup.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		shell.addControlListener(new ControlAdapter(){
			@Override
			public void controlResized(ControlEvent e) {
				Point size=shell.getSize();
			}			
		});
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				//System.exit(0);
			}
		});
		return true;
	}

	private String[] readConfigFile(String fileName) {
		File file = new File(fileName);
		if (!file.exists() || !file.isFile())
			return new String[0];
		try {
			FileReader reader = new FileReader(file);
			BufferedReader br = new BufferedReader(reader);
			List<String> serverList=new ArrayList<String>();	
			String str = null;
			while ((str = br.readLine()) != null) {
				serverList.add(str);
			}
			String[] servers=new String[serverList.size()];
			serverList.toArray(servers);
			return servers;
		} catch (Exception e) {
			MessageBox messageBox = new MessageBox(shell, SWT.OK);
			messageBox.setMessage("错误："+e);
			messageBox.open();
			return new String[0];
		}
	}


}
